{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Create Data\n",
    "\n",
    "This is the completed solution for the [Create Data Tutorial](https://developers.arcgis.com/labs/data/python/create-data/). \n",
    "\n",
    "[ArcGIS tutorials](https://developers.arcgis.com/labs/) are short guides demonstrating the three phases of building geospatial apps: Data, Design, Develop."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Please enter password: ········\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "from arcgis.gis import *"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To create the GIS object, we pass your profile that contains the url and your login credentials. Please replace the credentials below with that of your org. To learn more about profiles, see [here](https://developers.arcgis.com/python/guide/working-with-different-authentication-schemes/#Storing-your-credentialls-locally)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dev_gis = GIS('home')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Item title:\"Trailheads\" type:Feature Layer Collection owner:yjiang_geosaurus>]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_layer_srch_results = dev_gis.content.search(query='\"Trailheads\"', max_items=10)\n",
    "feature_layer_srch_results[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Trailheads'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trailheads_layer.properties.name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OBJECTID\n",
      "TRL_NAME\n",
      "PARK_NAME\n",
      "TRL_ID\n",
      "SEG_ID\n",
      "FEATURE\n",
      "FEAT_CMNT\n",
      "ELEV_FT\n",
      "CITY_JUR\n",
      "ZIP_CODE\n",
      "SUP_DIST\n",
      "X_STREET\n",
      "PARKING\n",
      "PHOTO\n",
      "LAT\n",
      "LON\n",
      "FID\n",
      "x\n",
      "y\n",
      "ObjectId2\n"
     ]
    }
   ],
   "source": [
    "for field in trailheads_layer.properties['fields']:\n",
    "    print('Name: {:16s}\\tType: {}'.format(field['name'], field['actualType']))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis import geometry\n",
    "from arcgis import features\n",
    "\n",
    "\n",
    "def create_feature(map, location):\n",
    "    try:\n",
    "        '''\n",
    "        `location` will be a dict the following format:\n",
    "        \n",
    "        ```json\n",
    "        {\n",
    "            'spatialReference': \n",
    "            {\n",
    "                'latestWkid': 3857, \n",
    "                'wkid': 102100\n",
    "            }, \n",
    "            'x': -13228260.775215352, \n",
    "            'y': 4044978.1647766223\n",
    "        }\n",
    "        ```\n",
    "        '''\n",
    "        \n",
    "        object_id = 1\n",
    "        point = geometry.Point(location)\n",
    "        \n",
    "        feature = features.Feature(\n",
    "            geometry=point, \n",
    "            attributes={\n",
    "                'OBJECTID': object_id,\n",
    "                'PARK_NAME': 'My Park',\n",
    "                'TRL_NAME': 'Foobar Trail',\n",
    "                'ELEV_FT': '5000'\n",
    "            }\n",
    "        )\n",
    "        \n",
    "        trailheads_layer.edit_features(adds=[feature])\n",
    "        map.draw(point)\n",
    "        \n",
    "    except Exception as e:\n",
    "        print(\"Couldn't create the feature. {}\".format(str(e)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "### Map of LA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<img src=></img>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "map.basemap = 'topo-vector'\n",
    "map.center = coords\n",
    "map.zoom = 11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "map.clear_graphics()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "map.add_layer(trailheads_layer)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "9.0"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
